package pub.xspace.nova.system.controller;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import pub.xspace.nova.common.controller.BaseController;
import pub.xspace.nova.common.domain.AjaxResult;
import pub.xspace.nova.system.domain.SysUser;
import pub.xspace.nova.system.service.SysUserService;

@RestController
@RequestMapping("/system/user")
@Slf4j
public class SysUserController extends BaseController {
	@Resource
	private SysUserService sysUserService;

	@GetMapping("/getalluser")
	public AjaxResult getAllUser() {
		return success(sysUserService.getAll(null));
	}

	@PostMapping("/getPage")
	public AjaxResult getPage(@RequestBody Map<String, Object> params) {
		SysUser sysUser = BeanUtil.toBean(params.get("sysUser"), SysUser.class);
		return success(sysUserService.getPage(Integer.valueOf(String.valueOf(params.get("pageNum"))),
				Integer.valueOf(String.valueOf(params.get("pageSize"))), sysUser));
	}

	@PostMapping("/save")
	public AjaxResult save(@RequestBody SysUser sysUser) {
		sysUser.setCreateUser("admin");
		sysUser.setOrgId("1");
		log.debug(sysUser.toString());
		sysUserService.save(sysUser);

		return success();
	}

	@PutMapping("/update")
	public AjaxResult update(@RequestBody SysUser sysUser) {
		sysUser.setUpdateUser("admin");
		sysUserService.updateById(sysUser);
		return success(sysUser);
	}

	@PutMapping("/updatePassword")
	public AjaxResult updatePassword(@RequestBody SysUser sysUser) {
		sysUser.setUpdateUser("admin");
		sysUserService.updatePasswordById(sysUser);
		return success();
	}

	@DeleteMapping("/deleteById")
	public AjaxResult deleteById(String id) {
		sysUserService.deleteById(id);
		return success();
	}

	@DeleteMapping("/deleteByIds")
	public AjaxResult deleteByIds(@RequestBody List<String> ids) {
		sysUserService.deleteByIds(ids);
		return success();
	}

	@GetMapping("/export")
	public void export(HttpServletResponse response) throws IOException {
		List<SysUser> sysUsers = sysUserService.getAll(null);

		ExcelWriter writer = ExcelUtil.getWriter(true);

		// writer.addHeaderAlias("username", "用户名");
		// writer.setOnlyAlias(false);
		writer.write(sysUsers, true);

		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
		String fileName = URLEncoder.encode("用户信息", "UTF-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
		ServletOutputStream os = response.getOutputStream();
		writer.flush(os);
		writer.close();
	}

	@PostMapping("/import")
	public AjaxResult importData(MultipartFile file) throws Exception {
		InputStream inputStream = file.getInputStream();
		ExcelReader reader = ExcelUtil.getReader(inputStream);
		// 设置别名，跟中文表头对应上
		// reader.addHeaderAlias("username","用户名")

		List<SysUser> list = reader.readAll(SysUser.class);
		for (SysUser sysUser : list) {
			sysUserService.save(sysUser);
		}

		return success();
	}
	
	@GetMapping("/barData")
	public AjaxResult getBarData() {
		Map<String,Object> map = new HashMap<>();
		List<SysUser> sysUsers = sysUserService.getAll(null);
		Set<String> orgSet = sysUsers.stream().map(SysUser::getOrgId).collect(Collectors.toSet());
		map.put("org", orgSet);
		List<Long> countList = new ArrayList<>();
		for(String orgId:orgSet) {
			long count = sysUsers.stream().filter(sysUser->sysUser.getOrgId().equals(orgId)).count();
			countList.add(count);
		}
		map.put("count", countList);
		return success(map);
	}
}
